/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved.
 *
 */

import path from 'path';

import { InputFile, OutputFile } from '@ohos/hvigor';
import fse from 'fs-extra';

import { BuildArtifactConst } from '../../const/build-directory-const.js';
import { OhosLogger } from '../../utils/log/ohos-logger.js';
import { OhosHapTask } from './ohos-hap-task.js';

/**
 * 测试框架执行mock时要把 build/default/intermediates/res/{default}/mock-config.json 。
 * mock-config.json 是由ets-loader生成到 intermediates 目录下的。
 * 拷贝到测试包 hap 中的resources/rawfile/mock/mock-config.json 。
 */
export abstract class TestCopyMockConfigJson extends OhosHapTask {
  private _log: OhosLogger = OhosLogger.getLogger(TestCopyMockConfigJson.name);

  @InputFile()
  protected get mockConfigJsonFile(): string {
    return this.pathInfo.getIntermediatesMockConfigJsonFile();
  }

  @OutputFile()
  protected get mockConfigJsonOutputFile() {
    return path.join(this.pathInfo.getIntermediatesResourcesRawFileMockDir(), BuildArtifactConst.MOCK_CONFIG_JSON);
  }

  protected doTaskAction(): void {
    this.copyMockConfigJson();
  }

  protected copyMockConfigJson(): void {
    if (fse.existsSync(this.mockConfigJsonFile)) {
      const rawFileMockPath = this.pathInfo.getIntermediatesResourcesRawFileMockDir();
      fse.ensureDirSync(rawFileMockPath);
      fse.copyFileSync(this.mockConfigJsonFile, this.mockConfigJsonOutputFile);
    } else {
      this._log.debug('Failed to generate mock-config.json during compilation.');
    }
  }
}
